package com.mapapp.flutter_mapapp.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;

import com.mapapp.flutter_mapapp.http.Api;
import com.mapapp.flutter_mapapp.http.Url;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.mapapp.flutter_mapapp.MainActivity;
import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.application.Constants;
import com.mapapp.flutter_mapapp.http.Url;
import com.mapapp.flutter_mapapp.persenter.LoginPersenter;
import com.mapapp.flutter_mapapp.util.MyActivityManager;
import com.mapapp.flutter_mapapp.util.SharePreferenceUtil;
import com.mapapp.flutter_mapapp.util.ToastUtils;
import com.mapapp.flutter_mapapp.view.LoginViewInterface;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends ImmersiveBaseActivity implements LoginViewInterface,View.OnClickListener{
    private static final String TAG = "LoginActivity";
    @BindView(R.id.et_login_username)
    EditText etLoginUsername;
    @BindView(R.id.et_login_password)
    EditText etLoginPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.tv_login_forget_password)
    TextView tvLoginForgetPassword;

    private LoginPersenter mPersenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        if (getIntent().getBooleanExtra(Constants.IS_TOKEN_ERROR, false)) {
            ToastUtils.showMessage(this, getString(R.string.token_error));
        }
        mContext = this;
        init();
        String host = (String) SharePreferenceUtil.get(getApplicationContext(), Constants.HOST, "");
        if(!"".equals(host)){
            Url.BASE_URL = host;
        }else{
            SharePreferenceUtil.put(getApplicationContext(), Constants.HOST, Url.BASE_URL);
        }
        mPersenter = new LoginPersenter(this,this,lifecycleSubject);
    }

    private void init(){
        btnLogin.setOnClickListener(this);
        String save_username = (String)SharePreferenceUtil.get(mContext, Constants.USERNAME,"");
        etLoginUsername.setText(save_username);
        String save_password = (String)SharePreferenceUtil.get(mContext, Constants.PASSWORD,"");
        etLoginPassword.setText(save_password);
        tvLoginForgetPassword.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                login();
                break;
            case R.id.tv_login_forget_password:
                Intent intent = new Intent(mContext, FindPasswordActivity.class);
                startActivityForResult(intent,0);
                break;
        }
    }

    private void login(){
        String username = etLoginUsername.getText().toString();
        if(TextUtils.isEmpty(username)){
            ToastUtils.showMessage(mContext, mContext.getString(R.string.username_not_null));
            return;
        }
        String password = etLoginPassword.getText().toString();
        if(TextUtils.isEmpty(etLoginPassword.getText().toString())){
            ToastUtils.showMessage(mContext, mContext.getString(R.string.password_not_null));
            return;
        }
        mPersenter.login(username,password);
    }

    @Override
    public void loginSuccess() {
        Intent intent = new Intent(mContext, MainActivity.class);
        startActivity(intent);
        MyActivityManager.getActivityManager().pop(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 222){
            etLoginUsername.setText("");
            etLoginPassword.setText("");
            SharePreferenceUtil.put(mContext, Constants.USERNAME, "");
            SharePreferenceUtil.put(mContext, Constants.PASSWORD, "");
        }
    }

    @OnClick(R.id.linear_setting)
    public void setHost(){
        final EditText editText = new EditText(mContext);
        editText.setText((String) SharePreferenceUtil.get(getApplicationContext(), Constants.HOST, ""));
        new AlertDialog.Builder(mContext)
                .setTitle("请输入服务器地址")
                .setView(editText)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Activity.INPUT_METHOD_SERVICE);
                        inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                        String host  = editText.getText().toString();
                        if(TextUtils.isEmpty(host)){
                            ToastUtils.showMessage(mContext, "服务器地址不能为空");
                            return;
                        }
                        if(!host.startsWith("http://")){
                            host = "http://" + host ;
                        }
                        SharePreferenceUtil.put(getApplicationContext(), Constants.HOST, host);
                        Url.BASE_URL = host;
                        Api.setApiService(null);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Activity.INPUT_METHOD_SERVICE);
                        inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                })
                .show();
    }
}
